﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class QFTMCOReport : BaseReport
    {
        string strCompany = "HLT";
        string strExcelFile = "";
        string sheetName = "";
       
        public QFTMCOReport()
            : base()
        {
        }
        public QFTMCOReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim(); 
      
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 7, int yStart = 1)
        {
            try
            {
                FillinHead();
                FillInStage(1,10);  
                FillInProcess(100, 9,true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInStage(int stage, int startrow)
        { 
            try
            {
                DataTable tbMaster = dsSource.Tables["QUTMASTER"];
                string cond = "";
                int row = startrow;
                DataRow[] rows = null;
                cond = "QUSTAGE='" + stage + "'";
                rows = tbMaster.Select(cond);
                DataRow rowx = null;
                int x=1,y=1;
                x=startrow;
                foreach (DataRow dwTemp in tbMaster.Rows)
                {
                    rowx = dwTemp;
                    exAgent.SetCellValue(x, 2, dwTemp["QUSTAGE"].ToString());
                    exAgent.SetCellValue(x, 4, dwTemp["QUSTAGEDES"].ToString());
                    exAgent.SetCellValue(x, 8, dwTemp["QUSOURCETYPE"].ToString());
                    exAgent.SetCellValue(x, 10, dwTemp["QUBOMID"].ToString());
                    exAgent.SetCellValue(x, 16, dwTemp["QUARTICLE"].ToString().Trim());
                    exAgent.SetCellValue(x, 20, dwTemp["QUVAR"].ToString().Trim());
                    exAgent.SetCellValue(x, 21, dwTemp["QUCOLOR"].ToString().Trim());
                    exAgent.SetCellValue(x, 24, dwTemp["QUDESS"].ToString().Trim());
                    exAgent.SetCellValue(x, 28, dwTemp["QUCOLORIT"].ToString().Trim());
                    exAgent.SetCellValue(x, 32, dwTemp["QUFINISH"].ToString().Trim());
                    exAgent.SetCellValue(x, 34, dwTemp["QUCPRICE"].ToString().Trim());
                    exAgent.SetCellValue(x, 36, dwTemp["QUCQUANTITY"].ToString().Trim());
                    exAgent.SetCellValue(x, 38, dwTemp["QUCVALUE"].ToString().Trim());
                    x++;
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        private void FillInProcess(int startrow,int stage,bool isadmin=false)
        {
            try
            {
                DataTable tbProcess= dsSource.Tables["QUPROCESS"]; 
                string cond = "";
                DataRow[] rows = null;
                DataRow[] rowb = null;
                cond = "QPSTAGE= '"+stage.ToString()+"'";
                rows = tbProcess.Select();
                int row = startrow;
                string art = "";
                string var = "";
                string color = "";
                string sbomid = "";

                foreach (DataRow dwTemp in rows)
                {
                    if (!isadmin)
                    {
                        exAgent.SetCellValue(row, 2, dwTemp["QPPROCDES"].ToString());
                        exAgent.SetCellValue(row, 11, dwTemp["QPCPRICE"].ToString());
                        exAgent.SetCellValue(row, 16, dwTemp["QPCVALUE"].ToString());
                        exAgent.SetCellValue(row, 21, dwTemp["QPSUPPLIER"].ToString());
                    }
                    else
                    {
                        exAgent.SetCellValue(row, 2, dwTemp["QPPROCDES"].ToString());
                        exAgent.SetCellValue(row, 7, dwTemp["QPCPRICE"].ToString());
                    }
                   row++;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillinHead()
        {
            try
            {
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                exAgent.SetCellValue(4, 4, filedate);
                DataRow dwVKKOPF = dsSource.Tables["VKKOPF"].Rows[0];
                exAgent.SetCellValue(5, 4, dwVKKOPF["VKKAUFKRS"].ToString() + dwVKKOPF["VKKAUFLFD"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void WriteExcelReport(string fileType)
        {
            try
            {
                string filename = "";
                string sheetName = "";
                string Standard = "MCOKN";
                strExcelFile = @"QFT\QFTStandard.xls";
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);
                if (!dsSource.Tables.Contains("VKKOPF"))
                {
                    return;
                }
                if (dsSource.Tables["VKKOPF"].Rows.Count == 0)
                    return;
                DataRow dwVKKOPF = dsSource.Tables["VKKOPF"].Rows[0];
                string Order = dwVKKOPF["VKKAUFKRS"].ToString().Trim() + "_" + dwVKKOPF["VKKAUFLFD"].ToString().Trim();
                exAgent.Open(filePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                filename = @"Cost\" + Order + ".xls";
                string file = Path.Combine(PathHelper.ApplicationPath, @"Report\" + filename);
                sheetName = DateTime.Now.ToShortTimeString().Replace(@":", "");
                if (System.IO.File.Exists(file))
                {
                    System.IO.File.Delete(file);
                }
                exAgent.CreateNewSheet(filename, Standard, sheetName);
                FillInExcelData(columns, dsSource, 7, 1);
                base.WriteExcelReport(fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
